package com.xiaozhen.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaozhen.common.core.domain.device.Device;
import com.xiaozhen.common.core.domain.device.DeviceCategory;
import com.xiaozhen.device.dao.DeviceCategoryDao;
import com.xiaozhen.device.dao.DeviceDao;
import com.xiaozhen.device.service.DeviceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * HISTORY
 *
 * @author zhenweisheng
 * @date 2021/11/1 11:31
 * @description:
 */
@Service
public class DeviceCategoryServiceImpl implements DeviceCategoryService {

    @Autowired
    private DeviceCategoryDao deviceCategoryDao;
    @Autowired
    private DeviceDao deviceDao;

    @Override
    public List<DeviceCategory> selectDeviceCategoryList(DeviceCategory deviceCategory) {
        QueryWrapper<DeviceCategory> queryWrapper = new QueryWrapper<>();
        if (deviceCategory.getCategoryName() != null) {
            queryWrapper.like("category_name", deviceCategory.getCategoryName());
        }
        List<DeviceCategory> deviceCategories = deviceCategoryDao.selectList(queryWrapper);
        //构建前端所需要的树
        return getChildPerms(deviceCategories, 0);
    }

    @Override
    public Boolean checkDeviceCategoryNameUnique(DeviceCategory deviceCategory) {
        QueryWrapper<DeviceCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("category_id");
        queryWrapper.eq("category_name", deviceCategory.getCategoryName());
        List<DeviceCategory> deviceCategories = deviceCategoryDao.selectList(queryWrapper);
        if (deviceCategories.size() == 0) {
            return true;
        } else if (deviceCategory.getCategoryId() != null && deviceCategory.getCategoryId().equals(deviceCategories.get(0).getCategoryId())) {
            return true;
        }
        return false;
    }

    @Override
    public Integer insertDeviceCategory(DeviceCategory deviceCategory) {
        return deviceCategoryDao.insert(deviceCategory);
    }

    @Override
    public Integer updateDeviceCategory(DeviceCategory deviceCategory) {
        return deviceCategoryDao.updateById(deviceCategory);
    }

    @Override
    public Boolean hasChildByDeviceCategoryId(Long deviceCategoryId) {
        QueryWrapper<DeviceCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", deviceCategoryId);
        Integer count = deviceCategoryDao.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }

    @Override
    public Boolean checkDeviceCategoryExistDevice(Long deviceCategoryId) {
        QueryWrapper<Device> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id", deviceCategoryId);
        Integer count = deviceDao.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }

    @Override
    public Integer deleteDeviceCategoryById(Long deviceCategoryId) {
        return deviceCategoryDao.deleteById(deviceCategoryId);
    }

    /**
     * 根据父节点的ID获取所有子节点
     *
     * @param list     分类表
     * @param parentId 传入的父节点ID
     * @return String
     */
    public List<DeviceCategory> getChildPerms(List<DeviceCategory> list, int parentId) {
        List<DeviceCategory> returnList = new ArrayList<>();
        for (Iterator<DeviceCategory> iterator = list.iterator(); iterator.hasNext(); ) {
            DeviceCategory t = iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getParentId() == parentId) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<DeviceCategory> list, DeviceCategory t) {
        // 得到子节点列表
        List<DeviceCategory> childList = getChildList(list, t);
        t.setChildren(childList);
        for (DeviceCategory tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<DeviceCategory> getChildList(List<DeviceCategory> list, DeviceCategory t) {
        List<DeviceCategory> tlist = new ArrayList<>();
        Iterator<DeviceCategory> it = list.iterator();
        while (it.hasNext()) {
            DeviceCategory n = it.next();
            if (n.getParentId().longValue() == t.getCategoryId().longValue()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<DeviceCategory> list, DeviceCategory t) {
        return getChildList(list, t).size() > 0;
    }
}
